package com.example.qxfw.dataBaseManager.nyzjk.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.ny.CropDistIndex;
import com.example.qxfw.dataBaseManager.nyzjk.service.DiseaseAndPestPredictionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/5  星期三   11:13
 * @description : 此类用来 病虫害指标库
 ***************************************/
@Slf4j
@Api(tags = "数据库管理-农业专家库-病虫害指标库")
@RestController
@CrossOrigin
@RequestMapping("/BchzbkController")
public class BchzbkController {

    @Autowired
    private DiseaseAndPestPredictionService diseaseAndPestPredictionService;

    @ApiOperation("根据作物名称降序查找作物名称")
    @RequestMapping(value = "selectByCropName")
    public SysResult selectByCropName(){
        List<CropDistIndex> cropDistIndices = this.diseaseAndPestPredictionService.selectByCropName();
        return SysResult.success(cropDistIndices);

    }

    @ApiOperation("根据作物名称升序查询")
    @RequestMapping(value = "getCropDistIndex")
    public SysResult getCropDistIndex(){
        List<CropDistIndex> cropDistIndex = this.diseaseAndPestPredictionService.getCropDistIndex();
        return SysResult.success(cropDistIndex);
    }

    @ApiOperation("根据作物名称和病虫害名称查询")
    @RequestMapping(value = "selectByCropNameAndDisdesesName")
    public SysResult selectByCropNameAndDisdesesName(String cropName,  String DiseasesName){
        List<CropDistIndex> cropDistIndexList = this.diseaseAndPestPredictionService.selectByCropNameAndDisdesesName(cropName, DiseasesName);
        return SysResult.success(cropDistIndexList);
    }

    @ApiOperation("根据起止时间查询")
    @RequestMapping(value = "selectByStartTimeAndEndTime")
    public SysResult selectByStartTimeAndEndTime(String startTime, String endTime){
        List<CropDistIndex> cropDistIndexList = this.diseaseAndPestPredictionService.selectByStartTimeAndEndTime(startTime, endTime);
        return SysResult.success(cropDistIndexList);
    }

    @ApiOperation("保存作物病虫害指标")
    @RequestMapping(value = "saveCropDistIndex")
    public SysResult saveCropDistIndex( CropDistIndex cropDistIndex){
        List<CropDistIndex> cropDistIndexList = this.diseaseAndPestPredictionService.selectByCropNameAndDisdesesNameNoOrder(cropDistIndex.getCropname(), cropDistIndex.getDiseasesname());
        if (cropDistIndexList.size()>0){
            return SysResult.fail("存在相同病虫害");
        }else {
            Integer insertCropDistIndex = this.diseaseAndPestPredictionService.insertCropDistIndex(cropDistIndex);
            if (insertCropDistIndex>0){
                return SysResult.success("保存成功");
            }else {
                return SysResult.fail("保存失败");
            }
        }
    }

    @ApiOperation("删除作物病虫害指标")
    @RequestMapping(value = "deleteCropDistIndex")
    public SysResult deleteCropDistIndex(String cropName,String diseasesName){
        List<CropDistIndex> cropDistIndexList = this.diseaseAndPestPredictionService.selectByCropNameAndDisdesesName(cropName, diseasesName);
        if (cropDistIndexList.size()>0){
            Integer deleteByCropNameAndDisdesesName = this.diseaseAndPestPredictionService.deleteByCropNameAndDisdesesName(cropName, diseasesName);
            if (deleteByCropNameAndDisdesesName>0){
                return SysResult.fail("删除成功");
            }else {
                return SysResult.success("删除成功");
            }
        }else {
            return SysResult.fail("数据库中没有此条数据，无法删除");
        }
    }

    @ApiOperation("更新作物病虫害指标")
    @RequestMapping(value = "updateCropDistIndex")
    public SysResult updateCropDistIndex(CropDistIndex cropDistIndex){
        Integer updateByCropNameAndDisdesesName = this.diseaseAndPestPredictionService.updateByCropNameAndDisdesesName(cropDistIndex);
        return SysResult.success();
    }

    /**导入*/



}
